package com.zsr.xmall.product.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhousiren
 * @description 线程池配置
 * @date 2022/8/21 16:19
 */
//@EnableConfigurationProperties(ThreadPoolConfigProperties.class)  //如果目标未使用@Component需开启
@Configuration
public class MyThreadConfig {

    /**
     * 配置线程池
     * @param pool 通过参数注入bean
     * @return
     */
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {
        return new ThreadPoolExecutor(pool.getCoreSize()
                , pool.getMaxSize()
                , pool.getKeepAliveTime()
                , TimeUnit.SECONDS
                , new LinkedBlockingDeque<>(10000)
                , Executors.defaultThreadFactory()
                , new ThreadPoolExecutor.AbortPolicy());
    }
}
